package com.framework.loippi.controller.medicine;

import com.framework.loippi.controller.GenericController;
import com.framework.loippi.entity.Pic;
import com.framework.loippi.entity.TCarousel;
import com.framework.loippi.entity.TMedicine;
import com.framework.loippi.service.*;
import com.framework.loippi.support.Message;
import com.framework.loippi.support.Pageable;
import com.framework.loippi.utils.ParameterUtils;
import com.framework.loippi.utils.StringUtil;
import com.mysql.jdbc.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

/**
 * Controller - 药品表
 *
 * @author wmj
 * @version 2.0
 */
@Controller("adminTMedicineController")
@RequestMapping({"/admin/medicine"})
public class TMedicineController extends GenericController {

    @Resource
    private TMedicineService tMedicineService;

    @Resource
    private PicService picService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private TCarouselService carouselService;

    @Resource
    private UserService userService;

    /**
     * 跳转添加页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = {"/add"}, method = {RequestMethod.GET})
    public String add(ModelMap model) {
        model.addAttribute("categories", categoryService.findAllCategory());
        return "/admin/medicine/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(TMedicine tMedicine, RedirectAttributes redirectAttributes) {
        DecimalFormat decimalFormat=new DecimalFormat("#.##");
        if (tMedicine.getPreferential() != null) {
            BigDecimal b=tMedicine.getPreferential().multiply(new BigDecimal(0.01));
            tMedicine.setPreferential(new BigDecimal(decimalFormat.format(b)));
        }

        // 最大优惠为50%
        tMedicine.setMaxpreferential(new BigDecimal(0.5));
        tMedicineService.save(tMedicine);
        if (tMedicine.getPicList() != null && tMedicine.getPicList().size() > 0) {
            for (String str : tMedicine.getPicList()) {
                Pic pic = new Pic();
                pic.setCreateTime(new Date());
                pic.setImgpath(str);
                pic.setProductId(tMedicine.getId());
                picService.save(pic);
            }
        }
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.jhtml";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable Long id, ModelMap model) {
        TMedicine tMedicine = tMedicineService.find(id);
        // 查询图片列表
        List<Pic> picList = picService.findByMedicineId(id);
        List<String> pics = new ArrayList<>();
        for (Pic pic : picList) {
            pics.add(pic.getImgpath());
        }
        tMedicine.setPicList(pics);
        model.addAttribute("tMedicine", tMedicine);
        model.addAttribute("categories", categoryService.findAllCategory());
        return "/admin/medicine/edit";
    }


    /**
     * 详情
     */
    @RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
    public String view(@PathVariable Long id, ModelMap model) {
        TMedicine tMedicine = tMedicineService.find(id);
        // 查询图片列表
        List<Pic> picList = picService.findByMedicineId(id);
        List<String> pics = new ArrayList<>();
        for (Pic pic : picList) {
            pics.add(pic.getImgpath());
        }
        tMedicine.setPicList(pics);
        model.addAttribute("tMedicine", tMedicine);
        model.addAttribute("categories", categoryService.findAllCategory());
        return "/admin/medicine/view";
    }


    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(TMedicine tMedicine, RedirectAttributes redirectAttributes) {
        DecimalFormat decimalFormat=new DecimalFormat("#.##");
        if (tMedicine.getPreferential() != null) {
            BigDecimal b=tMedicine.getPreferential().multiply(new BigDecimal(0.01));
            tMedicine.setPreferential(new BigDecimal(decimalFormat.format(b)));
        }

        // 最大优惠为50%
        tMedicine.setMaxpreferential(new BigDecimal(0.5));
        tMedicineService.update(tMedicine);
        //先删除之前的图片
        picService.deleteByMedicineId(tMedicine.getId());
        if (tMedicine.getPicList() != null && tMedicine.getPicList().size() > 0) {
            for (String str : tMedicine.getPicList()) {
                Pic pic = new Pic();
                pic.setCreateTime(new Date());
                pic.setImgpath(str);
                pic.setProductId(tMedicine.getId());
                picService.save(pic);
            }
        }
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.jhtml";
    }

    /**
     * 列表查询
     *
     * @param pageable
     * @param model
     * @return
     */
    @RequestMapping(value = {"/list"}, method = {RequestMethod.GET})
    public String list(Pageable pageable, HttpServletRequest request, ModelMap model) {

        Map<String, Object> paramter = ParameterUtils.getParametersMapStartingWith(request, "filter_");
        Map map = new HashMap();
        for (String key : paramter.keySet()) {
            if (!StringUtil.isEmpty(paramter.get(key).toString())) {
                map.put(key, paramter.get(key));
            }
        }
        String keyword = request.getParameter("keyword");
        String statusKey = request.getParameter("statusKey");
        if (!StringUtils.isNullOrEmpty(keyword)) {
            map.put("nameLike", keyword);
            map.put("stairNameLike", keyword);
        }
        if (!StringUtils.isNullOrEmpty(statusKey)) {
            map.put("status", statusKey);
        }
        map.put("order", "create_time desc");
        pageable.setParameter(map);
        model.addAttribute("paramter", paramter);
        model.addAttribute("page", this.tMedicineService.findByPage(pageable));
        return "/admin/medicine/list";
    }

    /**
     * 删除操作
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = {"/delete"}, method = {RequestMethod.POST})
    public @ResponseBody
    Message delete(Long[] ids) {
        this.tMedicineService.deleteAll(ids);
        return SUCCESS_MESSAGE;
    }
}
